﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Management;
using System.Diagnostics;

namespace VPNMonitor
{
	[RunInstaller(true)]
	public partial class ProjectInstaller : Installer
	{
		public ProjectInstaller()
		{
			InitializeComponent();
			
		}

		public override void Install(IDictionary stateServer)
		{
			Microsoft.Win32.RegistryKey system,
				currentControlSet,
				services,
				service;

			try
			{
				if (Context.Parameters["name"] != null)
				{
					this.serviceInstaller1.ServiceName = Context.Parameters["name"].ToString();
					this.serviceInstaller1.DisplayName = this.serviceInstaller1.ServiceName;
				}

				base.Install(stateServer);

				system = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("System");
				currentControlSet = system.OpenSubKey("CurrentControlSet");
				services = currentControlSet.OpenSubKey("Services");
				service = services.OpenSubKey(this.serviceInstaller1.ServiceName, true);
				service.SetValue("Description", "VPN monitoring service");
			}
			catch (Exception e)
			{
				Console.WriteLine("An exception was thrown during service installation:\n" + e.ToString());
			}
		}


		public override void Uninstall(IDictionary stateServer)
		{
			Microsoft.Win32.RegistryKey system,
				currentControlSet,
				services,
				service;

			try
			{
				if (Context.Parameters["name"] != null)
				{
					this.serviceInstaller1.ServiceName = Context.Parameters["name"].ToString();
					this.serviceInstaller1.DisplayName = this.serviceInstaller1.ServiceName;
				}

				system = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("System");
				currentControlSet = system.OpenSubKey("CurrentControlSet");
				services = currentControlSet.OpenSubKey("Services");
				service = services.OpenSubKey(this.serviceInstaller1.ServiceName, true);
			}
			catch (Exception e)
			{
				Console.WriteLine("Exception encountered while uninstalling service:\n" + e.ToString());
			}
			finally
			{
				base.Uninstall(stateServer);
			}
		}

		private void serviceInstaller1_AfterInstall(object sender, InstallEventArgs e)
		{
			
		}

		private void serviceInstaller1_Committed(object sender, InstallEventArgs e)
		{
			
		}
	}
}
